<?php

/**
 * Created by PhpStorm.
 * User: hhh
 * Date: 2016/10/25
 * Time: 15:14
 */
 define('DIR_IMAGE', 'D:/xampp/htdocs/image/');
 
class Product extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('product_m','product');
        $this->load->library('result');
    }

    function getProducts(){
//		$page=$this->uri->segment(3,1);
//		$num=$this->uri->segment(4,10);
//		$category=$this->uri->segment(5,-1);
////        $word=$this->uri->segment(6,"");
//        $sort=$this->uri->segment(6,"");
//        $word="";

        $page=$this->input->post('page');
        $num=$this->input->post('num');
        $category=$this->input->post('category');
        $word=$this->input->post('word');
        $sort=$this->input->post('sort');

        if($sort=="price"){
            $sort="p.price";
        }else if($sort=="quantity"){
            $sort="p.quantity";
        }else{
            $sort="pd.name";
        }

		$data=array('page'=>$page,'num'=>$num,'filter_name'=>$word,'category_id'=>$category,'sort'=>$sort);

        $results=$this->product->getProducts($data);
        if(isset($results)){
            $mess="数据获取成功";
            $code=0;
        }else{
            $mess="数据获取失败";
            $code=1;
        }
        $arr=array(
            'code'=>$code,
            'message'=>$mess,
            'data'=>$results
        );
        echo json_encode($arr);
    }
	
	function getProductDetail(){
		$product_id=$this->uri->segment(3,0);
		$data=Array();
		$data['tab_description'] = "详情";
		$data['tab_attribute'] = "属性";
		$data['text_related'] = "相关";
		$data['button_cart'] = "加入购物车";
		$data['button_wishlist'] = "收藏";
		$data['button_compare'] = "对比";

		$product_info = $this->product->getProduct($product_id);
		$data['attribute_groups'] = $this->product->getProductAttributes($product_id);
		
		$data['description'] = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');
		
		$this->load->model('ToolImage','tool_image');
		
			$data['products'] = array();
			$results = $this->product->getProductRelated($product_id);
			foreach ($results as $result) {
				
				if ($result['image']) {
					$image = $this->tool_image->resize($result['image'], 80, 80);
				} else {
					$image = $this->tool_image->resize('placeholder.png', 80, 80);
				}
				// if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					// $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
				// } else {
					// $price = false;
				// }
				// if ((float)$result['special']) {
					// $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
				// } else {
					// $special = false;
				// }
				// if ($this->config->get('config_tax')) {
					// $tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
				// } else {
					// $tax = false;
				// }
				// if ($this->config->get('config_review_status')) {
					// $rating = (int)$result['rating'];
				// } else {
					// $rating = false;
				// }
				$data['products'][] = array(
					'product_id'  => $result['product_id'],
					'thumb'       => $image,
					'name'        => $result['name'],
					'description' => html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'),
					// 'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
					'price'       => $result['price'],
					'special'     => $result['special'],
					'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
					'rating'      => $result['rating'],
					// 'href'        => $this->url->link('product/product', 'product_id=' . $result['product_id'])
					'href'        => 'product/getProduct/'. $result['product_id']
				);
			}
			
		//print_r($data);
		$this->load->view('product',$data);
	}

    function getProduct(){
        $id=$this->uri->segment(3,0);
        $results=$this->product->getProduct($id);

        if(isset($results)){
            $this->result->setData(0,"数据获取成功",$results);
        }else{
            $this->result->setData(1,"数据获取失败",null);
        }
        $this->result->output();
    }

    function getProductSpecials(){
        $results=$this->product->getProductSpecials();

        if(isset($results)){
            $this->result->setData(0,"数据获取成功",$results);
        }else{
            $this->result->setData(1,"数据获取失败",null);
        }
        $this->result->output();
    }

    function getLatestProducts(){
        $limit=$this->uri->segment(3,0);
        $results=$this->product->getLatestProducts($limit);

        if(isset($results)){
            $this->result->setData(0,"数据获取成功",$results);
        }else{
            $this->result->setData(1,"数据获取失败",null);
        }
        $this->result->output();
    }

    function getProductAttributes(){
        $id=$this->uri->segment(3,0);
        $results=$this->product->getProductAttributes($id);


        if(isset($results)){
            $this->result->setData(0,"数据获取成功",$results);
        }else{
            $this->result->setData(1,"数据获取失败",null);
        }
        $this->result->output();
    }

    function getProductImages(){
        $id=$this->uri->segment(3,0);
        $results=$this->product->getProductImages($id);

        if(isset($results)){
            $this->result->setData(0,"数据获取成功",$results);
        }else{
            $this->result->setData(1,"数据获取失败",null);
        }
        $this->result->output();
    }

    function getProductDiscounts(){
        $id=$this->uri->segment(3,0);
        $results=$this->product->getProductDiscounts($id);

        if(isset($results)){
            $this->result->setData(0,"数据获取成功",$results);
        }else{
            $this->result->setData(1,"数据获取失败",null);
        }
        $this->result->output();
    }

    function getProductOptions(){
        $id=$this->uri->segment(3,0);
        $results=$this->product->getProductOptions($id);

        if(isset($results)){
            $this->result->setData(0,"数据获取成功",$results);
        }else{
            $this->result->setData(1,"数据获取失败",null);
        }
        $this->result->output();
    }

}